import { defineConfig } from 'vite'
import { crx } from '@crxjs/vite-plugin'
import vue from '@vitejs/plugin-vue'
// unplugin-auto-import - 自动导入Vue和Element Plus的API，无需手动import
import AutoImport from 'unplugin-auto-import/vite'
// unplugin-vue-components - 自动导入Vue组件
import Components from 'unplugin-vue-components/vite'
// ElementPlusResolver - Element Plus的解析器，配合上面两个插件实现按需导入
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import manifest from './src/manifest.js'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const production = mode === 'production'

  return {
    build: {
      emptyOutDir: true,
      outDir: 'build',
      rollupOptions: {
        output: {
          chunkFileNames: 'assets/chunk-[hash].js',
        },
      },
    },
    plugins: [
      crx({ manifest }),
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    legacy: {
      skipWebSocketTokenCheck: true,
    },
    server: !production
      ? {
          host: 'localhost',
          port: 5173,
          hmr: {
            host: 'localhost',
            port: 5173,
          },
        }
      : undefined,
  }
})
